package com.coollang.smartbra.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Selection;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.base.MyApplication;


public class UIUtils {

    public static Context getContext() {
        return MyApplication.getApplication();
    }

    public static View inflate(int resId) {
        return LayoutInflater.from(getContext()).inflate(resId, null);
    }

    public static Resources getResources() {
        return getContext().getResources();
    }

    public static String getString(int resId) {
        return getResources().getString(resId);
    }

    public static Drawable getDrawable(int resId) {
        return getResources().getDrawable(resId);
    }

    public static int getColor(int resId) {
        return getResources().getColor(resId);
    }

    /**
     * 密码显示/隐藏
     */
    public static void seePassword(EditText editText, ImageView view) {
        editText.setSelected(!editText.isSelected());
        if (editText.isSelected()) { //密码可见
            editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            // 使光标始终在最后位置
            Editable etable = editText.getText();
            Selection.setSelection(etable, etable.length());
            view.setBackgroundResource(R.drawable.img_pass_able);
        } else {// 显示为密码
            editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            // 使光标始终在最后位置
            Editable etable = editText.getText();
            Selection.setSelection(etable, etable.length());
            view.setBackgroundResource(R.drawable.img_pass_enable);
        }
    }


    /**
     * 字数限制
     * @param editText
     * @param i
     */
    public static void setLengthFilter(EditText editText, int i) {
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(i)});
    }
}


